package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"dev.ixmxm.top/rms/model"
	"dev.ixmxm.top/rms/utils/json"
	"dev.ixmxm.top/rms/utils/log"
)

func PostHttp() {
	url := "http://127.0.0.1:8667/api/robot/agv_online"
	contentType := "application/json"
	data := `{"agv_id":"2","online_cmd":true}`
	resp, err := http.Post(url, contentType, strings.NewReader(data))
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	reply := &struct {
		Code    int    `json:"result_code"`
		Message string `json:"result_msg"`
	}{}
	if errBind := json.Bind(b, reply); errBind != nil {
		log.Errorf("bind http response body data error:%s", errBind.Error())
		return
	}
	if reply.Code == 0 {
		fmt.Println(string(b))
	}
}
func GetProjectInfo(ip, port string) []model.SlamMap {
	url := fmt.Sprintf("http://%s:%s/api/sync/pull_sync_project", ip, port)
	contentType := "application/json"
	data := `{"project_id":""}`
	resp, err := http.Post(url, contentType, strings.NewReader(data))
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return []model.SlamMap{}
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return []model.SlamMap{}
	}
	reply := &struct {
		Code int `json:"result_code"`
		Data struct {
			Projects []model.SlamMap `json:"projects"`
		} `json:"data"`
		Message string `json:"result_msg"`
	}{}
	if errBind := json.Bind(b, reply); errBind != nil {
		log.Errorf("bind http response body data error:%s", errBind.Error())
		return []model.SlamMap{}
	}
	if reply.Code == 0 {
		// fmt.Println(string(b))
		return reply.Data.Projects
	}
	return []model.SlamMap{}
}
func main() {
	// PostHttp()
	GetProjectInfo("192.168.167.52", "9002")
}
